package top.kuxiao.crmserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.kuxiao.crmserver.pojo.AccountManager;
import top.kuxiao.crmserver.pojo.Customer;
import top.kuxiao.crmserver.pojo.Services;
import top.kuxiao.crmserver.pojo.SysUser;
import top.kuxiao.crmserver.service.ICustomerService;
import top.kuxiao.crmserver.service.IServicesService;
import top.kuxiao.crmserver.utils.Auth;
import top.kuxiao.crmserver.utils.CookieUtil;
import top.kuxiao.crmserver.utils.ResultUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务创建  前端控制器
 * </p>
 *
 * @author xiaozhang
 * @since 2022-01-18
 */

@Api(tags = "服务管理")
@CrossOrigin
@RestController
@RequestMapping("/services")
public class ServicesController {

    @Autowired
    IServicesService servicesService;

    @Autowired
    CookieUtil cookieUtil;

    @Autowired
    ICustomerService customerService;

    /**
     * 客户经理创建服务
     * */

    @Auth("AM")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "服务类型"),
            @ApiImplicitParam(name = "outline",value = "概要"),
            @ApiImplicitParam(name = "requirement",value = "详细要求"),
            @ApiImplicitParam(name = "customerId",value = "客户编号")
    })
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "创建服务",notes = "服务是客户经理为客户创建的，需要数据表的非空字段，创建时间、状态、创建人不用")
    public ResultUtil add(HttpServletRequest request,
                          String type,
                          String outline,
                          String requirement,
                          Integer customerId
                          ){

        Services services = new Services();

        services.setServicesType(type);
        services.setServicesOutline(outline);
        services.setCustomerId(customerId);
        services.setServicesRequirement(requirement);

        services.setServicesState("新创建");

        SysUser uer = cookieUtil.getUer(request);

        services.setServicesCreator(uer.getName());
        services.setServicesCreationTime(new Date());

        if (servicesService.save(services)){
            return ResultUtil.resMsgOK("成功");
        }

        return ResultUtil.resMsgError("失败");
    }

    /**
     * 项目主管显示服务
     * */
    @Auth("SE")
    @RequestMapping(value = "seList",method = RequestMethod.POST)
    @ApiOperation(value = "显示全部服务信息",notes = "销售主管的权限")
    public ResultUtil seList(){

        List<Services> list = servicesService.list();

        for (Services services : list) {
            Customer byId = customerService.getById(services.getCustomerId());
            services.setCustomer(byId);
        }

        return ResultUtil.resObj(list);

    }


    /**
     * 项目主管分配服务
     * */

    @Auth("SE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "服务的id"),
            @ApiImplicitParam(name = "conductor",value = "处理人"),
    })
    @RequestMapping(value = "seUpdate/{id}",method = RequestMethod.PUT)
    @ApiOperation(value = "销售主管服务分配",notes = "销售主管的权限，只有状态为新创建的服务可以分配，只需要一个处理人（String）和服务id")
    public ResultUtil seUpdate(@PathVariable("id") Integer id, String conductor){

        Services services = servicesService.getById(id);
        services.setServicesConductor(conductor);
        services.setServicesAllocateTime(new Date());

        System.out.println(services.getServicesState());

        if (!"新创建".equals(services.getServicesState())){
            return  ResultUtil.resMsgError("不是新创建");
        }

        services.setServicesState("已分配");
        if (servicesService.updateById(services)){
            return ResultUtil.resMsgOK("成功");
        }
        return ResultUtil.resMsgError("失败");
    }

    /**
     * 客户经理显示分配给自己的服务
     * */
    @Auth("AM")
    @RequestMapping(value = "amList",method = RequestMethod.POST)
    @ApiOperation(value = "显示分配给自己的服务信息",notes = "客户经理使用")
    public ResultUtil amList(HttpServletRequest request){

        SysUser uer = cookieUtil.getUer(request);

        QueryWrapper<Services> servicesQueryWrapper = new QueryWrapper<>();

        servicesQueryWrapper.eq("services_conductor",uer.getName());

        List<Services> list = servicesService.list(servicesQueryWrapper);

        for (Services services : list) {
            Customer byId = customerService.getById(services.getCustomerId());
            services.setCustomer(byId);
        }

        return ResultUtil.resObj(list);

    }

    /**
     * 服务处理
     * */
    @Auth("AM")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "服务id"),
            @ApiImplicitParam(name = "note",value = "处理方法"),
    })
    @RequestMapping(value = "amUpdate",method = RequestMethod.PUT)
    @ApiOperation(value = "服务处理",notes = "客户经理使用,只需要处理方法和服务id")
    public ResultUtil amUpdate(Integer id,String note){

        Services services = servicesService.getById(id);

        services.setServicesHandling(note);


        services.setServicesProcessingTime(new Date());
        services.setServicesState("已处理");
        if (servicesService.updateById(services)){
            return ResultUtil.resMsgOK("成功");
        }
        return ResultUtil.resMsgError("失败");
    }

    /**
     * 服务反馈
     * */
    @Auth("AM")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "服务id"),
            @ApiImplicitParam(name = "note",value = "反馈信息"),
            @ApiImplicitParam(name = "satisfy",value = "满意度")
    })
    @RequestMapping(value = "feedback",method = RequestMethod.PUT)
    @ApiOperation(value = "客户的反馈",notes = "客户经理使用,只需要反馈信息、服务id和满意度")
    public ResultUtil feedback(Integer id,String note,Integer satisfy) {
        Services byId = servicesService.getById(id);

        if (!"已处理".equals(byId.getServicesState())) {
            return ResultUtil.resMsgError("请选择已处理的");
        }

        byId.setServicesCustomerFeedback(note);
        byId.setServicesSatisfaction(satisfy);
        byId.setServicesFeedbackTime(new Date());

        if (satisfy < 3) {
            byId.setServicesState("已分配");
        } else {
            byId.setServicesState("已归档");
        }

        if (servicesService.updateById(byId)) {
            return ResultUtil.resMsgOK("成功");
        }
        return ResultUtil.resMsgError("");
    }

    /**
     * 对已经归档的服务根据条件查询
     * */
    @Auth("SE,AM")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "服务类型"),
            @ApiImplicitParam(name = "customer",value = "客户编号"),
            @ApiImplicitParam(name = "handler",value = "处理人"),
            @ApiImplicitParam(name = "inquiry",value = "客户满意度")
    })
    @ApiOperation(value = "条件查询",notes = "可以根据,服务类型，客户编号，处理人，客户满意度查询")
    @RequestMapping(value = "query",method = RequestMethod.POST)
    public ResultUtil query(String type,Integer customer,String  handler,Integer inquiry){
        QueryWrapper<Services> servicesQueryWrapper = new QueryWrapper<>();

        servicesQueryWrapper.eq("services_state","已归档");

        if (type!=null && !"".equals(type)){
            servicesQueryWrapper.eq("services_type",type);
        }

        if (customer!=null){
            servicesQueryWrapper.eq("customer_id",customer);
        }

        if (handler!=null && !"".equals(handler)){
            servicesQueryWrapper.eq("services_conductor",handler);
        }

        if (inquiry!=null){
            servicesQueryWrapper.eq("services_satisfaction",inquiry);
        }

        List<Services> list = servicesService.list(servicesQueryWrapper);


        for (Services services : list) {
            Customer byId = customerService.getById(services.getCustomerId());

            services.setCustomer(byId);
        }

        return ResultUtil.resObj(list);
    }
}
